﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using System.Configuration;

namespace MedicareNoteManagerWeb
{
    public class backups //备份数据库
    {
        public void Execute()
        {
            try
            {
                //String command = "mysqldump --quick --host=localhost --default-character-set=gb2312 --lock-tables --verbose  --force --port=端口号 --user=用户名 --password=密码 数据库名 -r 备份到的地址";
                string path = ConfigurationManager.ConnectionStrings["routes"].ToString();
                string DBName = ConfigurationManager.ConnectionStrings["DBName"].ToString();
                string DBUserName = ConfigurationManager.ConnectionStrings["DBUserName"].ToString();
                string DBPwd = ConfigurationManager.ConnectionStrings["DBPwd"].ToString();
                string ServerIp = ConfigurationManager.ConnectionStrings["ServerIp"].ToString();
                string ServerPort = ConfigurationManager.ConnectionStrings["ServerPort"].ToString();
                string character = ConfigurationManager.ConnectionStrings["character"].ToString();
                //构建执行的命令
                StringBuilder sbcommand = new StringBuilder();
                StringBuilder sbfileName = new StringBuilder();
                sbfileName.AppendFormat("{0}", DateTime.Now.ToString()).Replace("-", "").Replace(":", "").Replace(" ", "");
                String fileName = sbfileName.ToString();
                String command = string.Format("mysqldump --quick --host={0} --default-character-set={1} --lock-all-tables --port={2} --user={3} --password={4} --databases {5} -R >" + path+"//"  + sbfileName + ".sql", ServerIp, character, ServerPort, DBUserName, DBPwd, DBName);
                //获取mysqldump.exe所在路径
                String appDirecroty = ConfigurationManager.ConnectionStrings["mysqldump"].ToString();
                Cmd.StartCmd(appDirecroty, command);
            }
            catch
            {
                Execute();
            }
        }

    }
}